/*
 * Article listing widget of Lukija RiL client.
 *
 * @author Yves Marcoz
 *
 * Copyright(C) 2011 Yves Marcoz <yvez@marcoz.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import Qt 4.7
import "storage.js" as Storage

Item {
    id: articleViewer
    width: parent.width;
    height: parent.height;

    property alias articleShown: articleView.visible;
    property bool zoomEnabled: false;
    property alias articles: articlesModel

    ListModel {
        id: articlesModel
        ListElement {
            title: "title1"
            articleid: "artid"
            unread: 0
            downloaded: 1
        }
    }

    function getCurrentUrl() {
        return articles.get(articleView.currentIndex).articleid;
    }

    function back() {
        if (articleView.visible) {
            articleView.visible = false;
        } else {
            Qt.quit()
            articleViewer.visible = false;
        }
    }

    ListView {
        id: articleList; model: visualModel.parts.list; z: 6
        width: parent.width; height: parent.height;
        cacheBuffer: 100;
        flickDeceleration: 1500
    }

    ListView {
        id: articleView; model: visualModel.parts.flip; orientation: ListView.Horizontal
        width: parent.width; height: parent.height; visible: false; z:8
        highlightRangeMode: ListView.StrictlyEnforceRange; snapMode: ListView.SnapOneItem
        highlightMoveDuration: 300;
    }

    Rectangle {
        id: noArticle
        anchors.centerIn: parent;
        visible: false;
        z:8;
        Text { id: noText; color: "#ffffff"; anchors.centerIn: parent; text: qsTr("No articles available"); }
        Image { id: loadingImage; anchors.centerIn: parent; source: "toolbar/images/loading.png";
            height: 96; width: 96;
            NumberAnimation on rotation {
                from: 0; to: 360; running: (loadingImage.visible == true); loops: Animation.Infinite; duration: 900
            }
        }

        states: [ State {
            name: "noArticle"; when: articles.count==0 && typeof(articles) !== 'undefined'
            PropertyChanges { target: noArticle; visible: true; }
            PropertyChanges { target: loadingImage; visible: false; }
            PropertyChanges { target: noText; visible: true; }
            }, State {
            name: "loading"; when: typeof(articles) === 'undefined'
            PropertyChanges { target: noArticle; visible: true; }
            PropertyChanges { target: noText; visible: false; }
            PropertyChanges { target: loadingImage; visible: true; }
            }
        ]
    }

    VisualDataModel {
        id: visualModel;
        delegate: Package {
                        id: packageItem
                        Item { id: flipItem; Package.name: 'flip';  width: articleViewer.width; height: articleViewer.height;

                            property string html: (articleView.visible && Math.abs(articleView.currentIndex-index)<2) ? Storage.getRilArticle(articleid) : ""
                            ArticleDisplay {
                                onPanningStarted: {
                                    articleView.positionViewAtIndex(articleView.currentIndex, ListView.Contain);
                                    articleView.interactive = false;
                                }

                                onPanningEnded: {
                                    articleView.interactive = true;
                                }
                            }
                        }

                        Item { Package.name: 'list';
                                id: wrapper; width: articleViewer.width; height: 86
                                Item {
                                    id: moveMe
                                    Rectangle { id: backRect; color: "black"; opacity: index % 2 ? 0.2 : 0.4; height: 84; width: wrapper.width; y: 1 }
                                    Text {
                                        anchors.fill: backRect
                                        anchors.margins: 5
                                        verticalAlignment: Text.AlignVCenter; text: title; color: (unread==1) ? "white" : "#7b97fd";
                                        width: wrapper.width; wrapMode: Text.WordWrap; font.bold: false;
                                        font.pixelSize: (utility.getPlatform()==="harmattan")?24:font.pixelSize
                                    }
                                }
                                MouseArea { anchors.fill: wrapper;
                                    onClicked: { articleView.positionViewAtIndex(index, ListView.Contain); articleView.visible = true; }
                                }
                        }
                    }
        model: articles
    }
}
